extends Node2D

var mob_timer:TimeCheck = TimeCheck.new(1000)
#子弹的对象池
var bullet_pool

var entity_scene = preload("res://scenes/main/entity/monster_base.tscn")
var bullet_scene = preload("res://scenes/main/fight/bullet.tscn")
var cur_level:int = 0;
#子弹数组
var bullet_arr = []
# Called when the node enters the scene tree for the first time.
func _ready():
	bullet_pool = Pool.new(10,"BULLET",bullet_scene);
	bullet_pool.killed.connect(on_pool_obj_done)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
	var _curTick = Time.get_ticks_msec();
	#print(_curTick)
	if  mob_timer.check_and_set(_curTick):
		time_create_monster()
		
func _physics_process(delta):
	pass

func time_create_monster():
	var new_monster = entity_scene.instantiate();
	new_monster.Init(1);
	#new_monster.position = new Vector2(get_viewport_rect().size.x * randf(),get_viewport_rect().size.y / 2)
	new_monster.position =  Vector2(get_viewport_rect().size.x * randf(),get_viewport_rect().size.y * randf());
	self.add_child(new_monster);
	
# 开始战斗
func game_start(level:int):
	print("游戏开始 lv="+String.num(level));
	cur_level = level;
	
func game_over():
	print("游戏结束")
	#for bullet:Node2d in bullet_arr:
		#bullet.du
	
func _input(event):
	if event is InputEventMouseButton:
		
		if event.button_index == MOUSE_BUTTON_LEFT:
			left_click(event, event.pressed);
			
		elif event.button_index == MOUSE_BUTTON_RIGHT:
			right_click(event, event.pressed);

func left_click(event, mouse_pressed:bool):
	print("Left ", event.position, "mouse_pressed ",mouse_pressed)
	if not mouse_pressed:
		var new_bullet:bullet_base = bullet_pool.get_one() as bullet_base;

		var bullet_data = BULLET_DATA.new();
		bullet_data.hp = 1
		bullet_data.attack = 1
		bullet_data.delay = 1000
		
		new_bullet.init(bullet_data)
		new_bullet.on_pool_obj_done = on_pool_obj_done;
		new_bullet.position = event.position;

		add_child(new_bullet);
	
func right_click(event, mouse_pressed:bool):
	print("右键", event.position, "mouse_pressed ",mouse_pressed)

#func create_single_bullet()
func on_pool_obj_done(target):
	target.hide();
